package com.ruoyi.web.controller.merchant;

import com.ruoyi.channel.service.IMpChannelMethodService;
import com.ruoyi.channel.service.IMpChannelService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.merchant.domain.MpMerchantMethod;
import com.ruoyi.merchant.service.IMpMerchantMethodService;
import com.ruoyi.merchant.service.IMpMerchantService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 *  信息操作处理
 * 
 * @author ruoyi
 * @date 2019-05-07
 */
@Controller
@RequestMapping("/merchant/mpMerchantMethod")
public class MpMerchantMethodController extends BaseController
{
    private String prefix = "merchant/mpMerchantMethod";
	
	@Autowired
	private IMpMerchantMethodService mpMerchantMethodService;
	@Autowired
	private IMpMerchantService mpMerchantService;
	
	@RequiresPermissions("merchant:mpMerchantMethod:view")
	@GetMapping()
	public String mpMerchantMethod()
	{
	    return prefix + "/allMerchantMethod";
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("merchant:mpMerchantMethod:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(MpMerchantMethod mpMerchantMethod)
	{
		startPage();
		List<MpMerchantMethod> list = mpMerchantMethodService.selectMpMerchantMethodList(mpMerchantMethod);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出列表
	 */
	@RequiresPermissions("merchant:mpMerchantMethod:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MpMerchantMethod mpMerchantMethod)
    {
    	List<MpMerchantMethod> list = mpMerchantMethodService.selectMpMerchantMethodList(mpMerchantMethod);
        ExcelUtil<MpMerchantMethod> util = new ExcelUtil<MpMerchantMethod>(MpMerchantMethod.class);
        return util.exportExcel(list, "MpMerchantMethod");
    }


	/**
	 * 新增
	 */
	@GetMapping("/add/{merchantId}")
	public String add(@PathVariable("merchantId") Long merchantId, ModelMap mmap)
	{
		mmap.put("merchant",mpMerchantService.selectMpMerchantById(merchantId));
	    return prefix + "/add";
	}


	/**
	 * 新增保存
	 */
	@RequiresPermissions("merchant:mpMerchantMethod:add")
	@Log(title = "", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(MpMerchantMethod mpMerchantMethod)
	{
		// 查看 当前商户-支付方法 是否添加该渠道-支付方法类型  有则不执行添加操作
		List<MpMerchantMethod> mpMerchantMethodList = mpMerchantMethodService.selectMpMerchantMethodByChannelMthodAndMerchant(mpMerchantMethod.getChannelMethodId(), mpMerchantMethod.getMerchantId());
		if (mpMerchantMethodList.size() == 0) {
			return mpMerchantMethodService.insertMpMerchantMethod(mpMerchantMethod);
		}else {
			return AjaxResult.error("渠道支付方法已存在");
		}

	}

	/**
	 * 修改
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		MpMerchantMethod mpMerchantMethod = mpMerchantMethodService.selectMpMerchantMethodById(id);
		mmap.put("mpMerchantMethod", mpMerchantMethod);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存
	 */
	@RequiresPermissions("merchant:mpMerchantMethod:edit")
	@Log(title = "", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(MpMerchantMethod mpMerchantMethod)
	{
		return mpMerchantMethodService.updateMpMerchantMethod(mpMerchantMethod);
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("merchant:mpMerchantMethod:remove")
	@Log(title = "", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(mpMerchantMethodService.deleteMpMerchantMethodByIds(ids));
	}



	/**
	 * 商户支付方法状态修改
	 */
	@Log(title = "商户支付方法信息", businessType = BusinessType.UPDATE)
	@RequiresPermissions("merchant:mpMerchantMethod:eidt")
	@PostMapping("/changeStatus")
	@ResponseBody
	public AjaxResult changeStatus(MpMerchantMethod mpMerchantMethod) {
		return toAjax(mpMerchantMethodService.changeStatus(mpMerchantMethod));
	}


	/**
	 * 校验商户-支付方法，不能出现相同优先级的商户-支付方法
	 */
	@PostMapping("/checkMerchantLevelUnique")
	@ResponseBody
	public String checkMerchantLevelUnique(MpMerchantMethod mpMerchantMethod) {
		return mpMerchantMethodService.checkMerchantLevelUnique(mpMerchantMethod);
	}



	/**
	 * 校验商户-支付方法，同一商户不能出现相同-渠道支付方法
	 */
	@PostMapping("/checkChannelMethodIdUnique")
	@ResponseBody
	public String checkChannelMethodIdUnique(MpMerchantMethod mpMerchantMethod) {
		return mpMerchantMethodService.checkChannelMethodIdUnique(mpMerchantMethod);
	}


	/**
	 * 查询商户已添加的渠道（去重查询）
	 */
	@PostMapping("/distinctChannel")
	@ResponseBody
	public TableDataInfo distinctChannel(Long merchantId){
		startPage();
		List<MpMerchantMethod> list = mpMerchantMethodService.selectDistinctChannelList(merchantId);
		return getDataTable(list);
	}


	/**
	 * 商户支付方法-设置批量费率
	 */
	@PostMapping("/methodSetting")
	@ResponseBody
	public AjaxResult methodSetting(int channelId, int merchantId, int channelMethodId, BigDecimal ChannelMethodPayRate, BigDecimal payRate, int status){
		int flag = payRate.compareTo(ChannelMethodPayRate);
		if (flag == 1) {
			return mpMerchantMethodService.updateMethodByParams(channelId, merchantId, channelMethodId, payRate, status);
		}else {
			return AjaxResult.error("商户的支付方法费率要大于渠道支付方法的费率");
		}
	}


	/**
	 * 支付方法 - 批量费率
	 */
	@GetMapping("/methodStatuSetting")
	public String methodStatuSetting() {
		return prefix + "/methodStatuSetting";
	}


	/**
	 * 商户支付方法-设置批量开关
	 */
	@PostMapping("/methodSwitchSetting")
	@ResponseBody
	public AjaxResult methodSwitchSetting(int channelId, int merchantId, int channelMethodId, int status){

		return mpMerchantMethodService.updateMethodBySwitchParams(channelId, merchantId, channelMethodId, status);

	}

}
